package com.woniuxy.oldmanapi.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.OlderGlu;
import com.woniuxy.oldmanapi.service.GluService;
import com.woniuxy.oldmanapi.vo.HeightVo;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import com.woniuxy.oldmanapi.vo.GluVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class GluController {

    @Autowired
    GluService gluService ;

    @GetMapping("/glu/list")
    public ResponseResult<PageInfo<GluVo>> list(
            @RequestParam(value = "now",defaultValue = "1") Integer now,
            @RequestParam(value = "size",defaultValue = "9") Integer size,
            String name){
        ResponseResult<PageInfo<GluVo>> result = new ResponseResult<>();
        try {
            PageInfo<GluVo> lists = gluService.findGluByPage(now,size,name);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(lists);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result ;
    }

    @PostMapping("/glu/add")
    public ResponseResult<Void> add(@RequestBody OlderGlu olderGlu){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            gluService.add(olderGlu);
            result.setCode(200);
            result.setMsg("添加成功!");
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("添加失败!");
        }
        return result ;
    }
    @PutMapping("/glu/modify")
    public ResponseResult<Void> modify(@RequestBody OlderGlu olderGlu){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            gluService.modify(olderGlu);
            result.setCode(200);
            result.setMsg("修改成功!");
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("修改失败!");
        }
        return result ;
    }
    @DeleteMapping("/glu/delete/{id}")
    public ResponseResult<Void> delete(@PathVariable("id")int id){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            gluService.delete(id);
            result.setCode(200);
            result.setMsg("删除成功!");
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("删除失败!");
        }
        return result ;
    }

    @GetMapping("/glu/one/{id}")
    public ResponseResult<GluVo> one(@PathVariable("id") int id) {
        ResponseResult<GluVo> result = new ResponseResult<>();
        try {
            GluVo gluVo = gluService.selectOne(id);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(gluVo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result;
    }
}
